package ird.demo.controller.transaction;

import ird.demo.bo.AjaxResult;
import ird.demo.common.DemoConstant;
import ird.demo.common.SysUtil;
import ird.demo.domain.Menu;
import ird.demo.service.MenuService;

import java.security.Principal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/transaction/IRSM0001")
public class IRSM0001Controller extends TranController {
	
	@Autowired
	private MenuService menuSvc;
	
	@Override
	protected String getTc() {
		return "IRSM0001";
	}

	@Override
	protected void preInput(ModelMap model) {
//		List<Menu> menuList = service.findAllMenu();
//		model.addAttribute("menuList", menuList);
		
	}
	
	@Override
	protected void preDetail(HttpServletRequest request, HttpSession session, ModelMap model) {
		String menuId = (String)request.getParameter("menuId");
		Map<String,String> keyParam = new HashMap<String,String>();
		keyParam.put("menuId", menuId);
		session.setAttribute(DemoConstant.KEY_PARAM, keyParam);
		Menu menu = menuSvc.findMenuById(menuId);
		model.addAttribute("menu", menu);
	}
	
	@RequestMapping("/getMenuList")
	@ResponseBody
	public String getMenuList(Model model){
		List<Menu> menuList = menuSvc.findAllMenu();
		model.addAttribute("menuList", menuList);
//		System.out.println("IRSM0001Controller.retrieveMenuList,menuList="+menuList);
		return gson.toJson(menuList);
	}

	@RequestMapping(value="/validAndUpd", method = RequestMethod.POST)
	@ResponseBody
	public String validAndUpd(@RequestBody Menu menu, Principal principal){
		System.out.println("==========validAndUpd, menu="+menu);
//		Map<String, String> resultMap = new HashMap<String,String>();
		
//		String menuId = paraMap.get("menuId");
//		String[][] fieldErrcodeArr = {
//			{"menuId","IRSM0001.mandatory.menuId"},
//		};
		
		AjaxResult result = null;;
		if(SysUtil.isNullstr(menu.getMenuId())){
			String errMsg = messageSource.getMessage("IRSM0001.mandatory.menuId", null, Locale.getDefault());
			result = new AjaxResult(DemoConstant.FAIL, "menuId", errMsg);
			return gson.toJson(result);
		}
		
		
		menu.setUpdateBy(principal.getName());
		menu.setUpdateTs(new Timestamp(new Date().getTime()));
		try{
			menuSvc.updateMenu(menu);
		}
		catch(Exception e){
			e.printStackTrace();
			String errMsg = messageSource.getMessage("system.genericErr", null, Locale.getDefault());
			result = new AjaxResult(DemoConstant.FAIL, "", errMsg);
			return gson.toJson(result);
		}
		
		result = new AjaxResult(DemoConstant.SUCCESS, null, null);
		return gson.toJson(result);
	}

	
}
